package com.zzg.processor;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;

public class FoodWorldProcessor implements PageProcessor {

	 // 抓取网站的相关配置，包括编码、重试次数、抓取间隔、超时时间、请求消息头、UA信息等
    private Site site= Site.me().setRetryTimes(3).setSleepTime(1000).setTimeOut(3000)
            .setUserAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.75 Safari/537.36");
	
	@Override
	public Site getSite() {
		// TODO Auto-generated method stub
		return site;
	}

	@Override
	public void process(Page page) {
		// TODO Auto-generated method stub
		Html html = page.getHtml();
		Document doc = html.getDocument();
		// 根节点
		Element root = doc.getElementById("J_list");
		Elements childern = root.getElementsByTag("li");
		for(Element ele : childern){
			// 查询指定的class名称【pic】
			//Element pic = ele.getElementsByClass("pic").first();
			// 查询div的class为pic的标签->a标签
			String title = ele.select("div.pic > a").first().attr("title");
			// 查询div的class为pic的标签 ->a 标签 ->img标签 的src 属性
			String path = ele.select("div.pic > a > img").first().attr("data-src");
			// 查询div的class为detail的标签 ->P标签 ->a标签的 文本内容
			String auth = ele.select("div.detail > p.subline > a").first().text();
			// 查询div的class为detail的标签 ->P标签 ->a标签的 href属性
			String auth_link = ele.select("div.detail > p.subline > a").first().attr("href");
			// 查询div的class为detail的标签 ->P标签 ->a标签的 href属性
			String content = ele.select("div.detail > p.subcontent").first().text();
			
			System.out.println("title is :" + title);
			System.out.println("path is:" + path);
			System.out.println("auth is:" + auth);
			System.out.println("auth_link is:" + auth_link);
			System.out.println("content is:" + content);
		}
	}

}
